package com.sysolve.betterCity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sysolve.betterCity.common.Help;
import com.sysolve.betterCity.model.Person;
import com.sysolve.betterCity.model.Place;
import com.sysolve.betterCity.service.BetterCityService;

/**
 * Servlet implementation class for Servlet: PointPlace
 *
 */
 public class PointPlace extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public PointPlace() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String domain = request.getParameter("domain");
		String userId = request.getParameter("userId");
		BetterCityService service = BetterCityService.getInstance();
		
		int lat = Help.getInt(request.getParameter("lat"));
		int lng = Help.getInt(request.getParameter("lng"));
		
		Person person = service.findPerson(service.findAccount(domain,userId));
		List<Place> places = service.findPlaceNearBy(person, lat, lng, 15);
		
		String address = Help.selectPlace(lng, lat);
		
		response.setContentType("text/html");  
		response.setCharacterEncoding("utf-8");
		
        PrintWriter out = response.getWriter();
        
        out.println(address);
        out.println("<hr>");
        
        if (places!=null && !places.isEmpty()) {
        	out.println("你的活动场所在这里吗？请选择一个");
        	for (Place p:places) {
        		out.println("<a href=\"javascript:void(0);\" onclick=\"setPlace("+p.getId()+",'"+p.getPlace()+"')\">");
        		out.println(p.getPlace());
        		out.println("</a>");
        	}
            out.println("<hr>");
        }
        
        out.close();
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}   	  	    
}